package com.experienceBaby.Cashbook.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.experienceBaby.Cashbook.R;
import com.experienceBaby.Cashbook.bean.Daily;
import com.experienceBaby.Cashbook.greendao.db.Recode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DailyAdapter extends BaseAdapter {
    private Context context;

    List<Daily> datas;


    public DailyAdapter(List<Daily> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (null != view) {
            viewHolder = (ViewHolder) view.getTag();
        } else {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.list_main, viewGroup, false);

            viewHolder.tv_date = view.findViewById(R.id.date);
            viewHolder.tv_cost = view.findViewById(R.id.cost);
            viewHolder.lv_single = view.findViewById(R.id.list_item);

            viewHolder.tv_date.setText(datas.get(i).getDate().toString());
            viewHolder.tv_cost.setText(datas.get(i).getCost() < 0 ? "支出：" : "收入：" + datas.get(i).getCost());

            List<Map<String, String>> data = new ArrayList<>();

            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
            


            for (Recode recode : datas.get(i).getDatas()) {
                Map<String, String> map = new HashMap<>();
                map.put("type", String.valueOf(R.drawable.shopping));
                map.put("remark", recode.getRemark());
                map.put("cost", String.valueOf(recode.getCost()));
                data.add(map);
            }
            Log.i("LISTSIZE", String.valueOf(data.size()));
            //TODO 单个条目点进去可以查看详情。
            viewHolder.lv_single.setAdapter(new SimpleAdapter(context, data, R.layout.list_single, new String[]{"type", "remark", "cost"}, new int[]{R.id.type, R.id.remark, R.id.cost}));
            view.setTag(viewHolder);
        }
        return view;
    }

    public static class ViewHolder {
        private TextView tv_date;
        private TextView tv_cost;
        private ListView lv_single;
    }
}
